<?php

namespace app\admin\model;

use think\Model;


class AreaFour extends Model
{

    

    //数据库
    protected $connection = 'database';
    // 表名
    protected $name = 'area_four';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    /**
     * @var array 4级对应区域id
     */
    public $typeField = [
        1 =>'province_id',
        2 =>'city_id',
        3 =>'county_id',
        4 =>'town_id'
    ];

    /**
     * @var array
     */
    public $listField = [
        1 =>'province_id,province_name',
        2 =>'city_id,city_name',
        3 =>'county_id,county_name',
        4 =>'town_id,town_name'
    ];

    /**
     * Notes: 省市区镇村单级列表
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/18 0018
     * Time: 14:47
     * @param $type : 1=省级,2=市级,3=区级,4=镇级
     * @param int $parent_id : parent level id
     * @return bool|\think\Paginator
     * @throws \think\exception\DbException
     */
    function _getLevelList($type,$parent_id=0){
        if($type>4){
            $this->error = "最多4级";
            return false;
        }
        $limit = input('limit',10);
        $where = [];
        if($parent_id){
            if($type>=2){
                $where[$this->typeField[$type-1]] = $parent_id;
            }else{
                $this->error = "级别错误";
                return false;
            }
        }
        $list = $this
            ->field($this->listField[$type])
            ->where($where)
            ->group($this->typeField[$type])
            ->cache(60)
            ->paginate($limit,false,['param'=>request()->param()]);
        return $list;
    }







}
